<%@ page language="java" contentType="text/html; charset=utf-8"	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/sys" prefix="sys"%>
<%@ taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>

<webui:panel title="角色信息" id="roleEditor" style="display:none">
	<webui:form method="get" styleClass="form-horizontal" id="roleEditorForm">
		<input type="hidden" value="" name="id" id="editFormRoleId" />
		<webui:input label="名称" name="name" id="editFormRoleName" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入2-4个中文字符" type="text" split="true">
			<webui:validator identity="Role:name" required="true" minlength="2" maxlength="10">
				{identity:'角色名称已存在'}
			</webui:validator>
		</webui:input>
		<webui:input label="描述" name="desc" id="editFormRoleDesc" labelClass="col-sm-2" inputClass="col-sm-10" 
		placeholder="请输入2-4个中文字符" type="text" split="true">
			<webui:validator required="true" minlength="2" maxlength="40"/>
		</webui:input>
		<div id="editFormPremission" class="form-group ">
			<label class="control-label col-sm-2"><span class="text-danger">*</span> 名称</label>
			<div class="col-sm-10">
				<div id="editOrgTree"></div>
			</div>
		</div>
		<div id="editFormPremission_split" class="hr-line-dashed"></div>		
	</webui:form>
	<div class="row">
		<div class="col-md-8 " style="float: right">
			<webui:button type="button" buttonClass="btn-white" onclick="cancelRole()">取消</webui:button>
			<webui:button id="roleSaveBtn" type="button" buttonClass="btn-primary" ladda="true" onclick="saveRole()">保存</webui:button>
		</div>
	</div>
</webui:panel>

<script>
	function createRole(){
		$("#editOrgTree").jstree("destroy");
		$('#editOrgTree').jstree({
			'core' : {
				'data' : {
						"url" : '${contextPath}/sys/permission/tree/99999999999.action',
						"data" : function (node) {
							return { "id" : node.id };
						}
					}
			}
			,"plugins" : ['checkbox', 'sort', 'types', 'wholerow', 'search', 'unique'],	  	
		});
		$('#editOrgTree').jstree(true).uncheck_all();
		tb_roles.create();
		$("#editFormPremission").show();
		$("#editFormPremission_split").show();
	}

	function resetValidateMsg(){
		roleEditorFormValidator.resetForm();
	}

	function editRole(id){
		tb_roles.create();
		$("#editFormPremission").hide();
		$("#editFormPremission_split").hide();
		
		$.ajax({
 	 	    type:"GET",
 	 	    async:false,
 	 	    url:"${contextPath}/rest/roles/"+id,
 	 	    success:function(data){
 	 	    	$("#editFormRoleId").val(data.id);
 	 	    	$("#editFormRoleName").val(data.name);
 	 	    	$("#editFormRoleDesc").val(data.desc);
 	 	    }
 	 	});
	}

	function cancelRole(){
		roleSaveBtn.ladda('stop');
		tb_roles.show();
	}

	function saveRole(){	
		if(roleEditorFormValidator.form()){
			roleSaveBtn.ladda('start');
			var obj = $('form#roleEditorForm').serializeObject();

			if(!$("#editFormPremission").is(":hidden")){
				var menus = $('#editOrgTree').jstree(true).get_checked(false);
				var resIds = new Array();
				obj.objType = "role";
				obj.resType = "menu";
				for(var i=0;i<menus.length;i++){
					resIds.push(menus[i]);
				}
				obj.resIds = resIds;
			}
			
			var json = JSON.stringify(obj);
			$.ajax({
				method:"post",
				url:'${contextPath}/sys/role/save.action',
				dataType:"json",
				contentType:"application/json; charset=utf-8",
				data:json,
				success:function(){
					roleSaveBtn.ladda('stop');
					showMessage('提示',
						($("#editFormRoleId").val()=="")?'保存成功。':'修改成功。',
						function(){
							$('#roleEditorForm').reset();
							tb_roles.refresh();
						}
					);
				}
			});
		}			
	}
</script>